<template>
  <div class="editable-cell">
    <div v-if="editable" class="editable-cell-input-wrapper">
      <a-input :value="value" @change="handleChange" @pressEnter="check" @blur="editable = false" />
    </div>
    <div v-else class="editable-cell-text-wrapper">
      {{ value || '--' }}
      <a-icon type="edit" class="editable-cell-icon" @click.stop="edit" v-if="showEdit" />
    </div>
  </div>
</template>

<script>
export default {
  name: 'EditableCell',
  props: {
    text: {
      type: String,
      default: '',
    },
    showEdit: {
      type: Boolean,
      default: true,
    },
    output: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      value: this.text,
      editable: false,
    }
  },
  methods: {
    handleChange(e) {
      const value = e.target.value
      this.value = value
    },
    check() {
      this.editable = false
      this.$emit('change', this.value)
    },
    edit() {
      // 输出设置权限
      if (this.output && !this.$hasPermission('scoreCardOutputName')) {
        this.$message.error('您没有权限编辑输出名称')
        return
      }
      this.editable = true
    },
  },
}
</script>

<style lang="less" scoped>
.editable-cell {
  .editable-cell-icon {
    color: @primary-color;
  }
}
</style>
